package com.epam.command.logic;

import java.sql.SQLException;

import com.epam.dao.DAOFactory;
import com.epam.dao.DAOFactory.DAOType;
import com.epam.dao.intf.DAO;
import com.epam.dao.intf.IHotelDAO;
import com.epam.dao.intf.ITouristVoucherDAO;
import com.epam.dao.intf.IUserDAO;
import com.epam.entity.Hotel;
import com.epam.entity.Hotel.Feeding;
import com.epam.entity.TouristVoucher;
import com.epam.entity.TouristVoucher.Country;
import com.epam.entity.TouristVoucher.Transport;
import com.epam.entity.TouristVoucher.Type;
import com.epam.entity.User;
import com.epam.entity.User.Role;

public class ManagerLogic {

	public static boolean createHotel(String name, int stars, boolean tv,
			boolean conditioning, Feeding feeding) {

		IHotelDAO hoteldao = (IHotelDAO) DAOFactory.INSTANCE.getDAO(DAOType.HOTEL);

		if (hoteldao.findByName(name) != (null)) {
			return false;
		} else {
			Hotel hotel = new Hotel.HotelBuilder().name(name).stars(stars)
					.tv(tv).conditioning(conditioning).feeding(feeding).build();
			try {
				hoteldao.insert(hotel);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		}
	}

	public static void createTouristVoucher(Country country, int duration,
			Type type, Transport transport, Hotel hotel, int cost,
			boolean lastMinute) {

		DAO tvd = DAOFactory.INSTANCE.getDAO(DAOType.TOURISTVOUCHER);

		TouristVoucher voucher = new TouristVoucher.TouristVoucherBuilder()
				.country(country).duration(duration).type(type)
				.transport(transport).hotel(hotel).cost(cost)
				.lastMinute(lastMinute).build();

		try {
			tvd.insert(voucher);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static boolean updateLastMinute(int id, String lastMinute, int cost) {

		ITouristVoucherDAO tvd = (ITouristVoucherDAO) DAOFactory.INSTANCE.getDAO(DAOType.TOURISTVOUCHER);
		boolean result;

		if (tvd.findByID(id) != null) {
			tvd.updateAttribute("last_minute", id, lastMinute);
			tvd.updateAttribute("cost", id, cost);

			result = true;
		} else {

			result = false;
		}

		return result;

	}
	
	public static boolean updateUser(String login, Role role) {
		
		IUserDAO userdao = (IUserDAO) DAOFactory.INSTANCE.getDAO(DAOType.USER);
		boolean result;
		
		if (userdao.findByLogin(login) != null) {
			User user = userdao.findByLogin(login);
			user.setRole(role);
			userdao.update(user);
			
			result = true;
		} else {
			result = false;
		}
		
		return result;

}
}